package com.ruoyi.web.asyserver;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

// 假设你已经构造好了 auth 和 requestJson
public class WxApiCaller {

    private static final String HOST = "https://api.mch.weixin.qq.com";

    public static String sendGet(String methodPath, String auth, CloseableHttpClient httpClient) throws Exception {
        HttpGet httpGet = new HttpGet(HOST + methodPath);
        httpGet.addHeader("Accept", "application/json");
        httpGet.addHeader("Authorization", auth);
        System.out.println(httpGet.getURI());

        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            HttpEntity resEntity = response.getEntity();
            return EntityUtils.toString(resEntity, "UTF-8");
        }
    }

    public static String sendPost(String methodPath, String auth, String requestJson, CloseableHttpClient httpClient) throws Exception {
        HttpPost httpPost = new HttpPost(HOST + methodPath);
        StringEntity reqEntity = new StringEntity(requestJson, ContentType.APPLICATION_JSON);
        httpPost.setEntity(reqEntity);
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("Authorization", auth);

        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            HttpEntity resEntity = response.getEntity();
            return EntityUtils.toString(resEntity, "UTF-8");
        }
    }
}
